package thn.glf.org.personalfinancial.objData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import thn.glf.org.lib.SQLiteDB;
import thn.glf.org.personalfinancial.Main;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class FinancialMonth {
	public static String TableName = "tblFinancialMonth";
	public static String Field_MonthID = "MonthID";
	public static String Field_MoneyUse = "MoneyUse";
	public static String Field_MonthFrom = "MonthFrom";
	public static String Field_MonthTo = "MonthTo";
	private int MonthID;
	private double MoneyUse;
	private String MonthFrom;
	private String MonthTo;
	private Context mContext;

	public Context getmContext() {
		return mContext;
	}

	public void setmContext(Context mContext) {
		this.mContext = mContext;
	}

	public FinancialMonth() {

	}

	public int getMonthID() {
		return MonthID;
	}

	public void setMonthID(int monthID) {
		MonthID = monthID;
	}

	public double getMoneyUse() {
		return MoneyUse;
	}

	public void setMoneyUse(double moneyUse) {
		MoneyUse = moneyUse;
	}

	public String getMonthFrom() {
		return MonthFrom;
	}

	public void setMonthFrom(String monthFrom) {
		MonthFrom = monthFrom;
	}

	public String getMonthTo() {
		return MonthTo;
	}

	public void setMonthTo(String monthTo) {
		MonthTo = monthTo;
	}

	public ArrayList<FinancialMonth> getList() {
		SQLiteDB db = new SQLiteDB(mContext);
		db.open();
		ArrayList<FinancialMonth> datalist = new ArrayList<FinancialMonth>();
		Cursor mCursor = db.query(true, TableName,
				new String[] { Field_MonthID, Field_MonthFrom, Field_MonthTo,
						Field_MoneyUse }, null, null, null, null,
				Field_MonthID, null);
		if (mCursor != null && mCursor.moveToFirst()) {
			do {
				FinancialMonth obj = new FinancialMonth();
				obj.setMonthID(mCursor.getInt(mCursor
						.getColumnIndex(Field_MonthID)));
				obj.setMonthFrom(mCursor.getString(mCursor
						.getColumnIndex(Field_MonthFrom)));
				obj.setMonthTo(mCursor.getString(mCursor
						.getColumnIndex(Field_MonthTo)));
				obj.setMoneyUse(mCursor.getDouble(mCursor
						.getColumnIndex(Field_MoneyUse)));
				datalist.add(obj);
			} while (mCursor.moveToNext());
		}
		db.close();
		return datalist;
	}

	public ArrayList<FinancialMonth> getListCurrent() {
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String currentdatetime = formatter.format(currentDate.getTime());
		int month = currentDate.get(Calendar.MONTH);
		int year = currentDate.get(Calendar.YEAR);
		int day = currentDate.get(Calendar.DAY_OF_MONTH);

		Calendar dateketso = Calendar.getInstance();

		dateketso.set(year, month, Main.ngayketso);
		String DateStart = formatter.format(dateketso.getTime());

		dateketso.add(Calendar.MONTH, 1);
		dateketso.add(Calendar.DAY_OF_MONTH, -1);
		String DateEnd = formatter.format(dateketso.getTime());
		String DateMin = currentdatetime.substring(0, 4) + "01"
				+ String.format("%02d", Main.ngayketso);
		String DateMax = currentdatetime;
		SQLiteDB db = new SQLiteDB(mContext);
		db.open();
		ArrayList<FinancialMonth> datalist = new ArrayList<FinancialMonth>();
		Cursor mCursor = db.query(true, TableName,
				new String[] { Field_MonthID, Field_MonthFrom, Field_MonthTo,
						Field_MoneyUse }, Field_MonthFrom + ">=? and "
						+ Field_MonthTo + "<=?", new String[] { DateMin,
						DateEnd }, null, null, Field_MonthID+" DESC", null);
		if (mCursor != null && mCursor.moveToFirst()) {
			do {
				FinancialMonth obj = new FinancialMonth();
				obj.setMonthID(mCursor.getInt(mCursor
						.getColumnIndex(Field_MonthID)));
				obj.setMonthFrom(mCursor.getString(mCursor
						.getColumnIndex(Field_MonthFrom)));
				obj.setMonthTo(mCursor.getString(mCursor
						.getColumnIndex(Field_MonthTo)));
				obj.setMoneyUse(mCursor.getDouble(mCursor
						.getColumnIndex(Field_MoneyUse)));
				datalist.add(obj);
			} while (mCursor.moveToNext());
		}
		db.close();
		return datalist;
	}

	public long Add(FinancialMonth obj) {
		SQLiteDB db = new SQLiteDB(mContext);
		db.open();
		ContentValues values = new ContentValues();
		values.put(Field_MonthID, obj.getMonthID());
		values.put(Field_MoneyUse, obj.getMoneyUse());
		values.put(Field_MonthFrom, obj.getMonthFrom());
		values.put(Field_MonthTo, obj.getMonthTo());

		long flag = db.Insert(TableName, values);
		db.close();
		return flag;
	}

	public boolean CheckExist() {
		boolean flag=false;
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		Calendar dateketso = Calendar.getInstance();
		dateketso.set(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), Main.ngayketso);
		String DateStart = formatter.format(dateketso.getTime());
		dateketso.add(Calendar.MONTH, 1);
		dateketso.add(Calendar.DAY_OF_MONTH, -1);
		String DateEnd = formatter.format(dateketso.getTime());
		SQLiteDB db = new SQLiteDB(mContext);
		db.open();
		Cursor mCursor = db.query(true, TableName,
				new String[] { Field_MonthTo,Field_MonthTo }, Field_MonthFrom + "=? and "
						+ Field_MonthTo + "=?", new String[] { DateStart,
				DateEnd }, null, null, null, null);
		if (mCursor != null && mCursor.moveToFirst()) {
			flag= true;
		}else{
			flag= false;
		}
		db.close();
		return flag;
	}
	public boolean CheckExist(String datecheck) {
		boolean flag=false;
		int year=Integer.parseInt(datecheck.substring(0,4));
		int month=Integer.parseInt(datecheck.substring(4,6));
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		Calendar dateketso = Calendar.getInstance();
		dateketso.set(year, month-1, Main.ngayketso);
		String DateStart = formatter.format(dateketso.getTime());
		dateketso.add(Calendar.MONTH, 1);
		dateketso.add(Calendar.DAY_OF_MONTH, -1);
		String DateEnd = formatter.format(dateketso.getTime());
		SQLiteDB db = new SQLiteDB(mContext);
		db.open();
		Cursor mCursor = db.query(true, TableName,
				new String[] { Field_MonthTo,Field_MonthTo }, Field_MonthFrom + "=? and "
						+ Field_MonthTo + "=?", new String[] { DateStart,
				DateEnd }, null, null, null, null);
		if (mCursor != null && mCursor.moveToFirst()) {
			flag= true;
		}else{
			flag= false;
		}
		db.close();
		return flag;
	}
}
